package com.zt.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.api.UserInfoFeign;
import com.zt.feign.UserFeign;
import com.zt.service.CardBindingService;
import com.zt.service.LoanService;
import com.zt.system.CardBinding;
import com.zt.system.Loan;
import com.zt.system.LoanApplication;
import com.zt.service.LoanApplicationService;
import com.zt.mapper.LoanApplicationMapper;
import com.zt.system.vo.LoanVO;
import com.zt.user.UserInfo;
import com.zt.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【loan_application】的数据库操作Service实现
* @createDate 2024-09-03 17:43:26
*/
@Service
public class LoanApplicationServiceImpl extends ServiceImpl<LoanApplicationMapper, LoanApplication>
    implements LoanApplicationService{
    @Resource
    private UserInfoFeign userInfoFeign;
    @Resource
    private UserFeign userFeign;
    @Resource
    private LoanService loanService;
    @Resource
    private CardBindingService cardBindingService;
    @Override
    @Transactional
    public Result loanApplicationSave(LoanVO loanVO) {
        System.err.println(loanVO);
        LoanApplication loanApplication = new LoanApplication();
        UserInfo userInfoUserId = userInfoFeign.getUserInfoUserId();
        loanApplication.setUserId(userInfoUserId.getUserId());
        loanApplication.setLoanAmount(loanVO.getLoanAmount());
        loanApplication.setApplicationDate(new Date());
        loanApplication.setLoanType(loanVO.getLoanType());
        loanApplication.setCardNumber(loanVO.getCardNumber());
        boolean save = this.save(loanApplication);
        return save?Result.success("成功"):Result.error("失败");
    }

    @Override
    public List<LoanApplication> loanApplicationFind() {
        List<LoanApplication> list = this.list();
        for (LoanApplication loanApplication : list) {
            UserInfo userInfoById = userFeign.getUserInfoById(loanApplication.getUserId());
            loanApplication.setUserInfo(userInfoById);
        }
        return list;
    }

    @Override
    @Transactional
    public Result loanApplicationByApplicationId(Integer applicationId) {
        LoanApplication one = this.getOne(Wrappers.<LoanApplication>lambdaQuery()
                .eq(applicationId != null, LoanApplication::getApplicationId, applicationId));
        if (one!=null) {
            one.setApplicationStatus(1);
            this.updateById(one);
        }
        CardBinding cardBinding = cardBindingService.CardBindingByCardNumber(one.getCardNumber());
        int i = one.getLoanAmount().intValue();
        cardBinding.setBalance(cardBinding.getBalance()+i);
        cardBindingService.updateById(cardBinding);
        Loan loan = new Loan();
        if (one.getLoanType()==1){
//            日日贷是0.69利率
            loan.setInterestRate(new BigDecimal(0.69));
        }else if (one.getLoanType()==2){
//            月月贷是0.39利率
            loan.setInterestRate(new BigDecimal(0.39));
        }else if (one.getLoanType()==3){
//            年年贷是0.09利率
            loan.setInterestRate(new BigDecimal(0.09));
        }
        loan.setCardNumber(cardBinding.getCardNumber());
        loan.setUserId(one.getUserId());
        loan.setLoanAmount(one.getLoanAmount());
        loan.setLoanStatus(0);
        loan.setLoanType(one.getLoanType());
        boolean save = loanService.save(loan);
        return save?Result.success("成功"):Result.error("失败");
    }

    @Override
    public Result loanApplicationByApplicationIdJuJue(Integer applicationId) {
        LoanApplication one = this.getOne(Wrappers.<LoanApplication>lambdaQuery()
                .eq(applicationId != null, LoanApplication::getApplicationId, applicationId));
        one.setApplicationStatus(2);
        boolean b = this.updateById(one);
        return b?Result.success("成功"):Result.error("失败");
    }
}




